//
//  EditorVC.m
//  MarianasTrench
//
//  Created by 付建国 on 16/3/19.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "EditorVC.h"
#import "EditorTableViewCell.h"
#import "ListModel.h"
#import "JGBoutiqueVC.h"

@interface EditorVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *backTableV;/**< 底层tableview */
@property (nonatomic, strong) NSMutableArray *modelArr;/**< model数组 */
@property (nonatomic, strong) NSString *path;/**< url字符串 */
@property (nonatomic, assign) BOOL isUpState;/**< 标记上拉刷新 */

@end

@implementation EditorVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.modelArr = [NSMutableArray array];
    
    if (_isOfKind == 1) {
        self.navigationItem.title = @"小编推荐";
    } else{
        self.navigationItem.title = @"精品听单";
    }
    
    self.isUpState = YES;
    
    [self createView];
//    [self dataHander];
    [self addHeader];
    [self addFooter];
}
#pragma mark - 创建UI
- (void)createView{
    self.backTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HETGHT - 113) style:UITableViewStylePlain];
    [self.view addSubview:_backTableV];
    self.backTableV.dataSource = self;
    self.backTableV.delegate =self;
    
    self.backTableV.rowHeight = 80;
    
    // 注册cell
    [self.backTableV registerClass:[EditorTableViewCell class] forCellReuseIdentifier:@"EditorTableViewCell"];
    
    
    
}
#pragma mark - tableview的协议方法
// 返回cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArr.count;
}
// 返回cell的样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EditorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditorTableViewCell"];
    
    ListModel *model = [[ListModel alloc] init];
    model = self.modelArr[indexPath.row];
    cell.model = model;
    
    return cell;
}
// cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_isOfKind == 1) {
        WJCollectionViewController *WJVC = [[WJCollectionViewController alloc] init];
        ListModel *model = [[ListModel alloc] init];
        model = self.modelArr[indexPath.row];
        WJVC.str = model.albumId;
        [self.navigationController pushViewController:WJVC animated:YES];
        
    }else if(_isOfKind == 2){
        ListModel *model = [[ListModel alloc] init];
        model = self.modelArr[indexPath.row];

        JGBoutiqueVC *boutiqueVC = [[JGBoutiqueVC alloc] init];
        boutiqueVC.specialId = model.specialId;
        
        [self.navigationController pushViewController:boutiqueVC animated:YES];
    }
}

#pragma mark - 数据解析
- (void)dataHander{
    if (_isOfKind == 1) {
        self.path = @"http://mobile.ximalaya.com/mobile/discovery/v1/recommend/editor?device=android&pageId=1&pageSize=20";
    }else if (_isOfKind == 2){
        self.path = @"http://mobile.ximalaya.com/m/subject_list?device=iPhone&page=1&per_page=20&position=&scale=2&title=%E6%9B%B4%E5%A4%9A";
;
    }
    [APPTools GETWithURL:_path par:nil success:^(id responseObject) {
        
        NSArray *arr = [responseObject objectForKey:@"list"];
        if (_isUpState == YES) {
            
            self.modelArr = [ListModel modelHandleWithArray:arr];
        }else{
            for (NSDictionary *dic in arr) {
                ListModel *model = [[ListModel alloc] init];;
                [model setValuesForKeysWithDictionary:dic];
                [self.modelArr addObject:model];
            }

        }
        
        [self.backTableV reloadData];
        [self.backTableV.mj_header endRefreshing];
        
    } filed:^(NSError *error) {
        NSLog(@"解析错误");
    }];
    
    
}

/**< 下拉刷新 */
- (void)addHeader{
    self.backTableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.isUpState = YES;
        [self dataHander];
        
    }];
    [self.backTableV.mj_header beginRefreshing];
    
}
/**< 上拉加载 */
- (void)addFooter{
    self.backTableV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
       
        self.isUpState = NO;
        [self dataHander];
        
    }];
    [self.backTableV.mj_footer beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
